package org.wheres;

import org.jivesoftware.smack.XMPPException;
import org.wheres.core.Communicator;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Login extends FragmentActivity {
	
	private Button loginButton;
	private Button newUserButton;
	private Button offlineButton;
	private EditText loginEditText;
	private EditText passwordEditText;
	private NoLocationDialog noLocationDialog;
	private Communicator communicatorService;
	private boolean boundToService = false;
	
	private Intent serviceIntent;
	
	private ServiceConnection communicatorConnection = new ServiceConnection(){

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			communicatorService = ((Communicator.CommunicatorBinder)service).getService();
			boundToService = true;
			
			
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			communicatorService = null;
			boundToService = false;
		}	
		
	};

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		loginButton = (Button)findViewById(R.id.Login);
		newUserButton = (Button)findViewById(R.id.NewUser);
		offlineButton = (Button)findViewById(R.id.offline);
		
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if(this.isNetworkAvailable() && this.isLocationAvailable()){
			loginButton.setEnabled(true);
			newUserButton.setEnabled(true);
			offlineButton.setEnabled(true);
			serviceIntent = new Intent(Login.this, Communicator.class);
			startService(serviceIntent);
			bindService(serviceIntent, communicatorConnection, Context.BIND_DEBUG_UNBIND);

			//TODO CHIUDERE L'ACTIVITY E LANCIARE LA CONTACT LIST ACTIVITY
		}else if (!this.isNetworkAvailable() && this.isLocationAvailable()){
			loginButton.setEnabled(false);
			newUserButton.setEnabled(false);
			offlineButton.setEnabled(true);
			Toast.makeText(this, R.string.NoNetworkOnLogin, Toast.LENGTH_LONG).show();
		}else if(!this.isLocationAvailable()){
			loginButton.setEnabled(false);
			newUserButton.setEnabled(false);
			offlineButton.setEnabled(false);
			noLocationDialog = NoLocationDialog.newIstance(R.string.NoPositionProviderDialogTitle);
			noLocationDialog.show(getSupportFragmentManager(), "noLocation");
		}
			
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		System.out.println(this.boundToService);
		if(this.communicatorConnection == null)
			System.out.println("null in login pause");
		if (this.boundToService)
			unbindService(communicatorConnection);
		if(this.communicatorConnection == null)
			System.out.println("null in login pause 2");
		System.out.println(this.boundToService);
		
	}
	
	
	
	private boolean isNetworkAvailable(){
		ConnectivityManager connManag = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo netInfo = connManag.getActiveNetworkInfo();
		if(netInfo != null){
			if(!netInfo.isConnectedOrConnecting())
				return false;
			else
				return true;
		}
		return false;
	}
	
	
	
	private boolean isLocationAvailable(){
		LocationManager locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		if (locManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || 
				locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
			return true;
		return false;
		
	}
	
	
	public void onClickLogin(View view){
			
		loginEditText = (EditText)findViewById(R.id.user);
		passwordEditText = (EditText)findViewById(R.id.pass);
		//TODO EditText settate solo per il testing. Eliminare a lavoro terminato.
		loginEditText.setText("michele");
		passwordEditText.setText("password");
		
		if(loginEditText.getText().toString().equals("") || passwordEditText.getText().toString().equals(""))
			Toast.makeText(this, R.string.UserOrPassEmpty, Toast.LENGTH_LONG).show();
		else{
			try {

				communicatorService.startConnection(loginEditText.getText().toString(),
													passwordEditText.getText().toString());
				startActivity(new Intent(this, ContactList.class));
				this.finish();
				//TODO INTENT PER ACTIVITY LISTA
			} catch (XMPPException e) {
				Toast.makeText(this, R.string.UserOrPassWrong, Toast.LENGTH_LONG).show();
			}
		}
		
	}
	
	public void onClickNewUser(View view){
		startActivity(new Intent(this, NewUser.class));
	}
	
	


}
